package com.baoxin.chenxixc4coach.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.baoxin.chenxixc4coach.CoachReferenceActivity;
import com.baoxin.chenxixc4coach.GenjinRecordActivity;
import com.baoxin.chenxixc4coach.H5TestActivity;
import com.baoxin.chenxixc4coach.MyCallback;
import com.baoxin.chenxixc4coach.PosterMouldActivity;
import com.baoxin.chenxixc4coach.R;
import com.baoxin.chenxixc4coach.ScheduleActivity;
import com.baoxin.chenxixc4coach.adapter.NewsAdapter;
import com.baoxin.chenxixc4coach.bean.News;
import com.baoxin.chenxixc4coach.java.MyHandler;
import com.baoxin.chenxixc4coach.java.ToWebListener;
import com.baoxin.chenxixc4coach.utils.Interfaces;
import com.baoxin.chenxixc4coach.utils.Msg;
import com.baoxin.chenxixc4coach.utils.NetUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static android.support.v7.widget.RecyclerView.SCROLL_STATE_IDLE;

public class IndexFragment extends BaseFragment implements ToWebListener {

    private TextView mB1;//咨询跟进
    private TextView mB2;//宣传海报
    private TextView mB3;//教练内参
    private TextView mB4;//日事清
    private RecyclerView recyclerView;
    private List<News> newsList;
    private NewsAdapter adapter;

    private SwipeRefreshLayout refreshLayout;

    private Handler handler;
    private int limit = 10;
    private int currentPage = 1;
    private int lastVisibleItem;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(activity).inflate(R.layout.fragment_index, container, false);

        handler = new MyHandler(this, activity) {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case Msg.REQUEST_SUCCEED:
                        ArrayList<News> newNews = (ArrayList<News>) msg.obj;
                        for (News o : newNews) {
                            newsList.add(o);
                        }
                        adapter.updateData(newsList, newNews.size() == 0 ? false : true);
                        break;
                }
            }
        };


        refreshLayout = view.findViewById(R.id.srl);
        refreshLayout.setColorSchemeResources(R.color.colorLightBlue);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                newsList.clear();
                updateData();
                refreshLayout.setRefreshing(false);
            }
        });
        mB1 = view.findViewById(R.id.zixungenjin);
        mB2 = view.findViewById(R.id.xuanchuanhaibao);
        mB3 = view.findViewById(R.id.jiaolianneican);
        mB4 = view.findViewById(R.id.rishiqing);

        mB1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //   Intent it = new Intent(activity, GenjinActivity.class);
                Intent it = new Intent(activity, GenjinRecordActivity.class);
                startActivity(it);
            }
        });

        mB2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(activity, PosterMouldActivity.class);
                startActivity(i);
            }
        });

        mB3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(activity, CoachReferenceActivity.class);
                startActivity(it);
            }
        });

        mB4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(activity, ScheduleActivity.class);
                startActivity(it);
            }
        });
        recyclerView = view.findViewById(R.id.recyclerview_news);
        adapter = new NewsAdapter(activity, this);
        updateData();
        final LinearLayoutManager layoutManager = new LinearLayoutManager(activity);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(activity,
                DividerItemDecoration.VERTICAL));
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == SCROLL_STATE_IDLE && lastVisibleItem == newsList.size()) {
                    updateData();
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = layoutManager.findLastVisibleItemPosition();
            }
        });
        return view;
    }

    private void updateData() {
        if (newsList == null) {
            newsList = new ArrayList<>();
        }
        JSONObject body = new JSONObject();
        try {
            body.put("page", currentPage);
            body.put("limit", limit);
            body.put("lable", "教练头条");
            NetUtil.post(activity, Interfaces.URL_HEAD_NEWS, body.toString(), new MyCallback(Interfaces.URL_HEAD_NEWS, handler));
        } catch (Exception e) {
            e.printStackTrace();
        }
        currentPage++;
    }

    @Override
    public void openWeb(String url) {
        H5TestActivity.startAction(activity, url);
    }
}
